﻿using System;
using System.IO;
using System.Text;

namespace Cef.Utility
{
    class GFunction
    {
        /// <summary>
        /// 将字节数组写入文件
        /// </summary>
        /// <param name="pi_strFileName">文件路径</param>
        /// <param name="pi_byData">写入字节数组</param>
        /// <param name="pi_iDataSize">字节数组大小</param>
        /// <returns></returns>
        static public bool tool_WriteOneFile(string pi_strFileName, byte[] pi_byData, int pi_iDataSize)
        {

            if (pi_iDataSize == 0)
            {
                return false;
            }

            FileStream fileStream = new FileStream(pi_strFileName, FileMode.Create);
            fileStream.Write(pi_byData, 0, pi_iDataSize);
            fileStream.Flush();
            fileStream.Close();

            return true;
        }

        /// <summary>
        /// 对BGR格式数据进行B、R转换，只支持24位深度图像
        /// </summary>
        /// <param name="pi_bySrc">bgr数据</param>
        /// <param name="pi_iSrcSize">bgr数据大小</param>
        /// <param name="po_byDst">转换后的rgb格式数据，需要开((pi_iWidth * 3 + 3) / 4) * 4 * pi_iHeight字节空间</param>
        /// <param name="pi_iDstSize">开辟空间大小</param>
        /// <param name="pi_iWidth">图片宽度（像素）</param>
        /// <param name="pi_iHeight">图片高度（像素）</param>
        /// <returns>>0 执行成功，函数执行成功后返回转换后的rgb格式数据大小</returns>
        static public int bgr2rgb(byte[] pi_bySrc, int pi_iSrcSize, byte[] po_byDst, int pi_iDstSize, int pi_iWidth, int pi_iHeight)
        {
            int iWidthSize = pi_iWidth * 3;
            int iDstWidthSize = ((pi_iWidth * 3 + 3) / 4) * 4;
            int iExternSize = ((pi_iWidth * 3 + 3) / 4) * 4 - pi_iWidth * 3;
            int iDstSize = iDstWidthSize * pi_iHeight;
            int iPosX = 0;
            int iPosY = 0;

            if (pi_iSrcSize != (iWidthSize * pi_iHeight))
            {
                return -1;
            }

            if (pi_iDstSize < iDstSize)
            {
                return -2;
            }

            for (iPosY = 0; iPosY < pi_iHeight; iPosY++)
            {
                for (iPosX = 0; iPosX < pi_iWidth * 3; iPosX += 3)
                {
                    po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 0] = pi_bySrc[iWidthSize * iPosY + iPosX + 2];
                    po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 1] = pi_bySrc[iWidthSize * iPosY + iPosX + 1];
                    po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 2] = pi_bySrc[iWidthSize * iPosY + iPosX + 0];
                }
            }

            return iDstSize;
        }

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="pi_bySrc">字节数组</param>
        /// <param name="pi_iSrcPosIdx">字节数组偏移量</param>
        /// <param name="pi_iSrcSize">转换字节数</param>
        /// <returns>16进制字符串</returns>
        public static string ByteArryToHexString(byte[] pi_bySrc, int pi_iSrcPosIdx, int pi_iCount)
        {
            int iPos = 0;
            StringBuilder strResult = new StringBuilder();

            for (iPos = 0; iPos < pi_iCount; iPos++)
            {
                strResult.Append(pi_bySrc[pi_iSrcPosIdx + iPos].ToString("X2"));
            }

            return strResult.ToString();
        }

        /// 16进制字符串转字节数组
        /// </summary>
        /// <param name="pi_strSrc">16进制字符串</param>
        /// <param name="po_byDstBuffer">字节数组Buffer</param>
        /// <param name="pi_iDstBufferSize">字节数组Buffer大小</param>
        /// <returns>实际转换大小</returns>
        public static int HexStringToByteArray(string pi_strSrc, byte[] po_byDstBuffer, int pi_iDstBufferSize)
        {
            int iPos = 0;
            int iDstSize = pi_strSrc.Length / 2;

            for (iPos = 0; (iPos < iDstSize) && (iPos < pi_iDstBufferSize); iPos++)
            {
                po_byDstBuffer[iPos] = Convert.ToByte(pi_strSrc.Substring(iPos * 2, 2), 16);
            }

            return iDstSize;
        }
    }
}
